package com.hy.controller.setting;

import com.hy.common.base.RespResult;
import com.hy.common.log.annotation.SysOpLogAnnotation;
import com.hy.entity.dto.UnitSearchDTO;
import com.hy.entity.pojo.HyUnit;

import com.hy.service.HyUnitService;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/unit")
public class UnitController {



   @Autowired
    private HyUnitService unitService;
    @GetMapping(value = "/showUnits")
    @SysOpLogAnnotation(menuName = "单位",menuBtn = "查询单位",reqParam = "'查询条件'")
    public RespResult showUnit( UnitSearchDTO dto){


        return unitService.showUnit(dto);

    }
    /**
     * 单位的添加
     */

    @PostMapping(value = "/addUnit")
    @SysOpLogAnnotation(menuName = "单位",menuBtn = "添加单位",reqParam = "'单位ID：'+#unit.unitId")
    public RespResult  addUnit(@RequestBody HyUnit unit){



        return unitService.addUnit(unit);
    }

    @PostMapping(value = "/deleteUnit")
    @SysOpLogAnnotation(menuName = "单位",menuBtn = "删除单位",reqParam = "'单位ID：'+#ids")
    public RespResult  addUnit(Integer[] ids){
        List<Integer> myIds=new ArrayList<>();
        Collections.addAll(myIds,ids);
        return unitService.deleteUnit(myIds);
    }

    @PostMapping(value = "/updateUnit")
    @SysOpLogAnnotation(menuName = "单位",menuBtn = "修改单位",reqParam = "'单位ID：'+#unit.unitId")
    public RespResult  updateUnit(@RequestBody HyUnit unit){
        if (unit.getDel().equals(2)){
            return RespResult.error(500,"不存在的资源");
        }
        return unitService.updateUnit(unit);
    }
}
